/*
 * Copyright (C) 2009 Leandro de Oliveira Aparecido <lehphyro@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.code.japa.repository.collection.jpa;

import org.springframework.orm.jpa.*;

import com.google.code.japa.annotation.*;
import com.google.code.japa.repository.collection.*;

/**
 * Factory of JPA persistent collection strategies.
 * 
 * @author Leandro Aparecido
 * @since 1.0
 * @see com.google.code.japa.repository.collection.PersistentCollectionStrategy
 * @see com.google.code.japa.repository.collection.jpa.FetchAllJpaPersistentCollectionStrategy
 * @see com.google.code.japa.repository.collection.jpa.FetchOnDemandJpaPersistentCollectionStrategy
 * @see com.google.code.japa.repository.collection.jpa.FetchBufferedJpaPersistentCollectionStrategy
 */
public final class JpaPersistentCollectionStrategyFactory {

	/**
	 * This class implements the singleton design pattern.
	 * <p>Instantiation is not allowed.
	 */
	private JpaPersistentCollectionStrategyFactory() {
	}

	/**
	 * Creates a new instance of the required strategy.
	 * 
	 * @param <T> Type of the entity the strategy will manipulate.
	 * @param entityType Type of the entity the strategy will manipulate.
	 * @param jpaOperations Helper to execute JPA operations. 
	 * @param fetchStrategy Strategy type the returned strategy must implement.
	 * @param fetchSize Fetch size for strategies that support/require it.
	 * @return Created strategy.
	 */
	public static <T> PersistentCollectionStrategy<T> create(Class<T> entityType,
															 JpaOperations jpaOperations,
															 FetchStrategy fetchStrategy,
															 int fetchSize) {
		switch (fetchStrategy) {
			case ALL:
				return new FetchAllJpaPersistentCollectionStrategy<T>(entityType, jpaOperations);
			case ONDEMAND:
				return new FetchOnDemandJpaPersistentCollectionStrategy<T>(entityType, jpaOperations);
			case BUFFERED:
				return new FetchBufferedJpaPersistentCollectionStrategy<T>(entityType, jpaOperations, fetchSize);
			default:
				throw new IllegalArgumentException("Unknown fetch strategy: " + fetchStrategy);
		}
	}
}
